package io.sdph.statistics.dao;

import io.sdph.core.database.Transaction;
import io.sdph.core.database.exceptions.DatabaseException;

import io.sdph.statistics.model.OrdersStatistic;
import io.sdph.statistics.model.ProductsStatistic;
import io.sdph.statistics.model.UsersStatistic;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

@Repository
public class StatisticsManagerDB implements StatisticsManagerDAO  {

	static final Logger log = Logger.getLogger(StatisticsManagerDB.class);

	@Override
	public void saveUsersStatistic(final UsersStatistic usersStatistic) {
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					int amount=0;
					ResultSet rs = select("select count(Userr.idUserr) as liczbaUrzytkownikow from Userr");
					if(rs.next()){
						amount = rs.getInt("liczbaUrzytkownikow");
					}
					insert("INSERT INTO UserrStatistic VALUES (?,?);", usersStatistic.getDateUser(),amount);
					
					return null;
				}
			};
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public UsersStatistic getUsersStatistic(final Date date) {

		UsersStatistic userStatistic = null;
		
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					UsersStatistic userStatistic = new UsersStatistic(null);
					
					ResultSet rs = select("select distinct * from UserrStatistic where StatUdate= ?",String.valueOf(date));
					if(rs.next()){
						userStatistic.setDateUser(rs.getDate("StatUdate"));
						userStatistic.setAmountUser(rs.getLong("StatUamount"));
					}
					
					return userStatistic;
				}
			};
			userStatistic = (UsersStatistic) tr.getResult();
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return userStatistic;
	}

	@Override
	public void saveOrdersStatistic(final OrdersStatistic ordersStatistic) {
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					int amount=0;
					ResultSet rs = select("select count(Orderr.numberOrderr) as liczbaZamowien from Orderr");
					if(rs.next()){
						amount = rs.getInt("liczbaZamowien");
					}
					insert("INSERT INTO OrderStatistic VALUES (?,?);", ordersStatistic.getDateOrder(),amount);
					
					return null;
				}
			};
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public OrdersStatistic getOrdersStatistic(final Date date) {
		
		OrdersStatistic orderStatistic = null;
			
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					OrdersStatistic orderStatistic = new OrdersStatistic(null);
					
					ResultSet rs = select("select distinct * from OrderStatistic where StatOdate= ?",String.valueOf(date));
					if(rs.next()){
						orderStatistic.setDateOrder(rs.getDate("StatUdate"));
						orderStatistic.setAmountOrder(rs.getLong("StatUamount"));
					}
					
					return orderStatistic;
				}
			};
			orderStatistic=(OrdersStatistic) tr.getResult();
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return orderStatistic;
	}

	@Override
	public void saveProductsStatistic(final ProductsStatistic productsStatistic) {

		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					int amount=0;
					ResultSet rs = select("select count(Product.idProduct) as liczbaProdyktow from Product");
					if(rs.next()){
						amount = rs.getInt("liczbaProdyktow");
					}
					insert("INSERT INTO ProductStatistic VALUES (?,?,?);",productsStatistic.getIdProduct() ,productsStatistic.getDateProduct(),amount);
					
					return null;
				}
			};
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public ProductsStatistic getProductsStatistic(final Date date, final String id) {
		ProductsStatistic productStatistic = null;
		
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					ProductsStatistic productStatistic = new ProductsStatistic(null);
					
					ResultSet rs = select("select distinct * from ProductStatistic where StatUdate= ? and idProduct= ? ",String.valueOf(date),id);
					if(rs.next()){
						productStatistic.setAmountProduct(rs.getLong("StatPamount"));
						productStatistic.setDateProduct(rs.getDate("StatPdate"));
						productStatistic.setIdProduct(rs.getString("idProduct"));
						
					}
					
					
					return productStatistic;
				}
			};
			productStatistic = (ProductsStatistic) tr.getResult();
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return productStatistic;
	}

	@Override
	public List<ProductsStatistic> getProductsStatistics(String id) {
		List<ProductsStatistic> listaProdStat = new ArrayList<ProductsStatistic>();
		try {
			Transaction tr = new Transaction() {
				
				@Override
				public Object doIt() throws DatabaseException, SQLException {
					List<ProductsStatistic> listaProdStat = new ArrayList<ProductsStatistic>();

					ResultSet rs = select("select * from ProductStatistic ");
					ProductsStatistic prodStat = null;
					
					while (rs.next()) {
						
						prodStat= new ProductsStatistic(rs.getDate("StatPdate"));
						prodStat.setAmountProduct(rs.getLong("StatPamount"));
						
						prodStat.setIdProduct(rs.getString("idProduct"));
						
						
						listaProdStat.add(prodStat);
					}
					
					return listaProdStat;
				}
			};
			listaProdStat = (List<ProductsStatistic>) tr.getResult();
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return listaProdStat;
	}
	
	

}
